//
//  TypeCheckingViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/18.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation

private class MideaItem {
    var name : String
    init(name : String){
        self.name = name
    }
}

private class Song : MideaItem {
    var artist : String
    init(name : String,artist : String) {
        self.artist = artist
        super.init(name: name)
    }
}

private class Movie : MideaItem {
    var director : String
    init(name: String,director : String) {
        self.director = director
        super.init(name: name)
    }
}

class TypeCheckingViewController : BaseViewController {
    
    fileprivate let library = [Song.init(name: "Lion", artist: "Andy"),
        Movie.init(name: "2015", director: "Jack"),
        Song.init(name: "Mountain", artist: "Lucy"),
        Movie.init(name: "2014", director: "Lee"),
        Movie.init(name: "2016", director: "John"),]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        checkType()
        downcasting()
        anyObejctType()
        anyType()
    }
    
    fileprivate func checkType(){
        
        var SongCount = 0
        var MovieCount = 0
        
        for item in library {
            if item is Song {
                SongCount += 1
            }else if item is Movie {
                MovieCount += 1
            }
        }
        
        print("There are " + String(SongCount) + " songs and " + String(MovieCount) + " movies")
    }
    
    fileprivate func downcasting(){
        
        for i in 0 ..< library.count {
            let item = library[i]
            if let song = item as? Song {
                print("Index is " + String(i) + ", song's name is " + song.name + " and artist is " + song.artist)
            }else if let movie = item as? Movie {
                print("Index is " + String(i) + ", movie's name is " + movie.name + " and director is " + movie.director)
            }
        }
    }
    
    fileprivate func anyObejctType(){
        let someObject : [AnyObject] = [
            Movie.init(name: "2015", director: "Jack"),
            Movie.init(name: "2014", director: "Lee"),
            Movie.init(name: "2016", director: "John"),
        ]
        
        for item in someObject {
            let movie = item as! Movie
            print("someObject : movie's name is " + movie.name + " and director is " + movie.director)
        }
    }
    
    fileprivate func anyType(){
        var things : Array<Any> = Array.init(repeating: 0, count: 0)
        
        things.append(1)
        things.append(0.0001)
        things.append("hello world")
        things.append([1,2])
        things.append(["key":"value"])
        
        printArray(things)
        
        for thing in things {
            switch thing{
            case is Int:
                print("Int is " + String(describing: thing))
            case is Double:
                print("Double is " + String(describing: thing))
            case is String:
                print("String is " + String(describing: thing))
            default:
                print("thing is " + String(describing: thing))
            }
        }
    }
    
    fileprivate func printArray(_ array : Array<Any>) {
        print(array)
    }
}
